package com.example.app10_listview_demo;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app10_listview_demo.adapter.AppAdapter;
import com.example.app10_listview_demo.adapter.AppInfo;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener {

    private AppAdapter appAdapter;

    private List<AppInfo> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化ListView
        ListView lvMain = findViewById(R.id.lv_main);
        data = getAllAppInfos();
        appAdapter = new AppAdapter(this, data);
        lvMain.setAdapter(appAdapter);
        // 给ListViewItem设置点击监听事件
        lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             * Item点击事件
             * @param parent ListView
             * @param view 当前行的Item视图对象
             * @param position 当前行的位置下标
             * @param id The row id of the item that was clicked.
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 提示当前行的应用名称
                if (!data.isEmpty()) {
                    AppInfo appInfo = data.get(position);
                    Toast.makeText(MainActivity.this, appInfo.getAppName(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 给ListViewItem设置长按监听事件
        lvMain.setOnItemLongClickListener(this);
    }

    /**
     * 获取Android系统中的AppInfo信息
     * @return 系统中的所有应用信息
     */
    private List<AppInfo> getAllAppInfos() {
        List<AppInfo> list = new ArrayList<>(10);
        // 获取应用的packageManager
        PackageManager packageManager = getPackageManager();
        // 创建一个主界面的Intent
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 获取包含应用的信息列表
        // 需要开启 QUERY_ALL_PACKAGES 权限
        List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);

        if (resolveInfoList.isEmpty()) {
            throw new RuntimeException("未获取到应用信息。");
        }

        // 遍历
        for (ResolveInfo resolveInfo : resolveInfoList) {
            // 获取包名
            String packageName = resolveInfo.activityInfo.packageName;
            // 获取图标
            Drawable icon = resolveInfo.loadIcon(packageManager);
            // 获取应用名称
            String appName = resolveInfo.loadLabel(packageManager).toString();
            list.add(new AppInfo(icon, appName, packageName));
        }

        return list;
    }

    /**
     * ListView 的长按监听事件
     * @param parent The AbsListView where the click happened
     * @param view The view within the AbsListView that was clicked
     * @param position The position of the view in the list
     * @param id The row id of the item that was clicked
     *
     * @return true，阻止事件冒泡
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // 删除当前行数据
        data.remove(position);
        // 更新列表
        appAdapter.notifyDataSetChanged();
        return true;
    }
}